<?php
declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Service\GoodsService;
use App\Service\RechargeService;
use App\Service\UserService;

#[AutoController]
class RechargeController extends BaseController
{
    #[Inject]
    private GoodsService $goodsService;
    #[Inject]
    private RechargeService $rechargeService;
    #[Inject]
    private UserService $userService;

    public function goos_list(RequestInterface $request,ResponseInterface $response){
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $list_data = $this->rechargeService->get_user_goods_list($applet_id,$user_id);
        return $this->resultResponse(["data"=>$list_data]);
    }
    public function order_back(RequestInterface $request,ResponseInterface $response){
        $order_id = $request->input('order_id', '');
        if($order_id) {
            $res = $this->rechargeService->order_back($order_id);
            if($res == true) {
                return $this->resultResponse(["data"=>$res]);
            } else {
                return $this->resultResponse(["code"=>20004]);
            }
        } else {
            return $this->resultResponse(["code"=>20003]);
        }
    }

    public function buy(RequestInterface $request,ResponseInterface $response){
        # 下单
        $applet_id = $this->get_applet_id();
        $playlet_id = $request->input('playlet_id', 0);


        $xc_open_id = $request->input('xc_open_id', '');
        $goods_id = $request->input('recharge_goods_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($goods_id)){
            return $this->resultResponse(["code"=>20050]);
        }

        $list_data = $this->rechargeService->buy($applet_id,$user_id,$goods_id,$playlet_id,1);
        return $this->resultResponse(["data"=>$list_data]);
    }

}